
/*
 * Copyright 2014 Hieu Rocker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hani.momanii.supernova_emoji_library.Handler;

import hani.momanii.supernova_emoji_library.ResourceTable;
import hani.momanii.supernova_emoji_library.emoji.Emojicon;
import hani.momanii.supernova_emoji_library.emoji.People;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.Arrays;

public class EmojiconListContainer {
    Component rootView;
    private final ArrayList<Emojicon> mData;
    private EmojiconRecents mRecents;

    /**
     * 构造
     *
     * @param context 上下文
     * @param emojicons 数据
     * @param recents 接口
     * @param emojiconsPopup 软键盘
     * @param textField 输入框
     */
    public EmojiconListContainer(Context context, ArrayList<Emojicon> emojicons, EmojiconRecents recents, EmojiconsPopup emojiconsPopup, EmojiconTextField... textField) {
        setRecents(recents);
        rootView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_emojicon_grid, null, false);
        ListContainer gridView = (ListContainer) rootView.findComponentById(ResourceTable.Id_Emoji_GridView);

        TableLayoutManager tableLayoutManager = new TableLayoutManager();
        tableLayoutManager.setColumnCount(10);
        tableLayoutManager.setOrientation(ListContainer.HORIZONTAL);

        gridView.setLayoutManager(tableLayoutManager);

        if (emojicons == null) {
            mData = People.DATA;
        } else {
            mData = emojicons;
        }
        EmojiProvider mAdapter = new EmojiProvider(context, mData);
        gridView.setItemProvider(mAdapter);
        gridView.setItemClickedListener((listContainer, component, i, l) -> {
            Emojicon mDatum = mData.get(i);
            if (mRecents != null) {
                mRecents.addRecentEmoji(rootView.getContext(), mDatum, textField);
            }
        });
    }

    private void setRecents(EmojiconRecents recents) {
        mRecents = recents;
    }
}